import { Component, OnInit, ViewChild, Input, Output, EventEmitter } from '@angular/core';

@Component({
  selector: 'modal-groupname',
  templateUrl: 'modalgroupname.component.html',
  styleUrls: ['modalgroupname.component.less']
})
export class ModalGroupNameComponent implements OnInit {
  @Input() title: string;
  @Output() onConfirmed = new EventEmitter<any>();
  @Output() onHide = new EventEmitter<any>();
  groupName: string;
  @ViewChild("inputRef") inputRef;
  constructor() { }

  ngOnInit() {
    this.inputRef.nativeElement.focus();
  }

  confirm(val: string) {
    if (val) {
      this.groupName = val.length > 12 ? val.slice(0, 12) : val;
      this.onConfirmed.emit(val);
      this.closeModal();
    } else {
      this.onConfirmed.emit("");
      this.closeModal();
    }
  }

  closeModal() {
    this.onHide.emit(true);
  }
}